#include "header.h"

/* This function removes the last character of a string in place and also 
   returns the altered value.
*/

char *chop(char *str) 
{
  *(str + (char)(strlen(str) - 1)) = '\0';
  return str;
}

/* This function works like the Perl "chomp" to chop the string only if its 
   last character is \n.
*/

char *chomp(char *str)
{
  if (*(str + (char)(strlen(str) - 1)) == '\n')
    return chop(str);
  else
    return str;
}

/* Generic constructors and destructors */

enemy::enemy(void) 
{
  name = (char *) calloc(50, 1);
}

enemy::~enemy(void) 
{
  destroy_bitmap(pic);
  free(name);
}

/* This function reads data in from the file referenced by filename and 
   loads the appropriate stats into the appropriate variables in the parent 
   enemy object. It returns 0 on success, -1 on error (bad file contents).
*/

int enemy::load_enemy(char *filename) 
{
  int dtype, idata, iscor = 0;
  char *data = (char *)calloc(50, 1), *field = (char *)calloc(50, 1);
  if ((FILE *aefile = fopen(filename, "r")) == NULL) {
    errno = EACCES;
    return -1;
  char *curline = (char *) calloc(50, 1);
  while (fgets(curline, 50, aefile) != NULL) {
    chop(curline);
    if (!iscor) {
      if (strcmp(curline, "archipelago enemy data file")) {
	return -1;
      }
      else {
	iscor = 1;
	continue;
      }
    }
    else {
      dtype = get_enem_data(curline, data, field);
      if (dtype == -1) 
	return -1;
      if (dtype == DTCOM)
	continue;
      if (dtype == DTINT) {
	idata = atoi(data);
      }
      if (!strcmp(field, "health"))
	setHealth(idata);
      else if (!strcmp(field, "speed"))
	setSpeed(idata);
      else if (!strcmp(field, "damage"))
	setDamage(idata);
      else if (!strcmp(field, "wps"))
	setWps(idata);
      else if (!strcmp(field, "range"))
	setRange(idata);
      else if (!strcmp(field, "name"))
	strcpy(name, data);
      else if (!strcmp(field, "bmp-path"))
	pic = load_bitmap(data, NULL);
    }
  }
  free((void *)field);
  free((void *)data);
  return 0;
}

int enemy::save_enemy(char *filename) 
{
  if ((fopen("nosuchfile.foobar", "r") == NULL) && (errno == ENOENT)) 
    printf("Success!\n");
  return 0;
}
  

/* This function takes a string in AEF format and returns the data type.
   It takes the string and 2 pointers to char, in which are passed back the
   data and data field. Data types are 0 or DTINT for integer, 1 or DTSTR for 
   string, and 2 or DTCOM for comment (ignored). The function returns -1 on 
   error (invalid input format).
*/

int enemy::get_enem_data(char *datstr, char *retval, char *stat) {
  if (strstr(datstr, "health") == datstr) {
    strcpy(retval, (datstr + 7));
    strcpy(stat, "health");
    return DTINT;
  }
  else if (strstr(datstr, "speed") == datstr) {
    strcpy(retval, (datstr + 6));
    strcpy(stat, "speed");
    return DTINT;
  }
  else if (strstr(datstr, "range") == datstr) {
    strcpy(retval, (datstr + 6));
    strcpy(stat, "range");
    return DTINT;
  }
  else if (strstr(datstr, "damage") == datstr) {
    strcpy(retval, (datstr + 7));
    strcpy(stat, "damage");
    return DTINT;
  }
  else if (strstr(datstr, "wps") == datstr) {
    strcpy(retval, (datstr + 4));
    strcpy(stat, "wps");
    return DTINT;
  }
  else if (strstr(datstr, "name") == datstr) {
    strcpy(retval, (datstr + 5));
    strcpy(stat, "name");
    return DTSTR;
  }
  else if (strstr(datstr, "bmp-path") == datstr) {
    strcpy(retval, (datstr + 5));
    strcpy(stat, "bmp-path");
    return DTSTR;
  }
  else if (strstr(datstr, "#") == datstr)
    return DTCOM;
  return -1;
}
/* Function get_enem_data ends here. */


/* This is a test main function for the purposes of testing the enemy class
   and interface. If this file is compiled with others relating to Archipelago,
   this main function should be commented out. Only uncomment if you are 
   testing or using the enemy class alone.
*/

int main(int argc, char** argv) {
  enemy *newenem = new enemy;
  char *filename = (char *)calloc(50, 1);
  filename = "data/testfile.aef";
  newenem->save_enemy(filename);
  /*  if (newenem->load_enemy(filename) != 0) {
    fprintf(stderr, "Error loading file: %s\n", filename);
    exit(-1);
  }
  else
    fprintf(stderr, "Successfully loaded file: %s\n", filename); 
    printf("Health: %d\nWps: %d\nRange: %d\nName: %s\n", newenem->getHealth(), newenem->getWps(), newenem->getRange(), newenem->getName());*/
  return 0;
}
